package cn.z2huo.usercenter.authentication.app.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

/**
 * <p>
 *
 * @author z2huo
 */
@Configuration
@EnableScheduling
@ComponentScan(basePackages = "cn.z2huo.usercenter.authentication.app")
public class SchedulingConfig implements SchedulingConfigurer {

//    @Bean(value = "CustomThreadPoolTaskScheduler", destroyMethod = "shutdown")
//    @Primary
    public ThreadPoolTaskScheduler getScheduling() {
        ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();

        threadPoolTaskScheduler.setPoolSize(Runtime.getRuntime().availableProcessors());
        threadPoolTaskScheduler.setWaitForTasksToCompleteOnShutdown(true);
        threadPoolTaskScheduler.setAwaitTerminationSeconds(60);
        threadPoolTaskScheduler.setThreadNamePrefix("custom-task-scheduler-");

        threadPoolTaskScheduler.initialize();
        return threadPoolTaskScheduler;
    }

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.setTaskScheduler(getScheduling());
    }
}
